﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:BAM="http://schemas.microsoft.com/BizTalkServer/2004/10/BAM"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

  <xsl:param name="ServiceName" />
  <xsl:param name="ActivityName" />
  <xsl:param name="NamespaceName" />
  
  <xsl:variable name="lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>
  <xsl:variable name="upper">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

  <xsl:template match="BAM:BAMDefinition">
    using System;
    using System.Runtime.Serialization;
    using System.ServiceModel;

    namespace <xsl:value-of select="$NamespaceName"/>
    {
      <xsl:call-template name="ServiceContract" />
      <xsl:call-template name="DataContract" />
    }

  </xsl:template>

  <xsl:template name="DataContract">
    [DataContract]
    public class  <xsl:value-of select="$ServiceName"/>Type
    {
      <xsl:apply-templates select="BAM:Activity[@Name = $ActivityName]/BAM:Checkpoint" />
    }
  </xsl:template>

  <xsl:template match="BAM:Checkpoint">
    <xsl:variable name="Type">
      <xsl:choose>
        <xsl:when test="@DataType='DATETIME'">DateTime </xsl:when>
        <xsl:when test="@DataType='NVARCHAR'">string </xsl:when>
        <xsl:when test="@DataType='FLOAT'">decimal </xsl:when>
        <xsl:when test="@DataType='INT'">int </xsl:when>
      </xsl:choose>
    </xsl:variable>

    
    <xsl:value-of select="$Type" /><xsl:value-of select="translate(@Name,$upper,$lower)" />;

    [DataMember]
    public <xsl:value-of select="$Type" /><xsl:value-of select="@Name" />
    {
      get { return <xsl:value-of select="translate(@Name,$upper,$lower)" />; }
      set { <xsl:value-of select="translate(@Name,$upper,$lower)" /> = value; }
    }

  </xsl:template>



  <xsl:template name="ServiceContract">
    [ServiceContract]
    public interface I<xsl:value-of select="$ServiceName" />
    {
      [OperationContract]
      void LogCompleteActivity(string activityId, <xsl:value-of select="$ServiceName"/>Type <xsl:value-of select="$ServiceName"/>Type);

      [OperationContract]
      void BeginActivity(string activityId);
      
      [OperationContract]
      void UpdateActivity(string activityId, <xsl:value-of select="$ServiceName"/>Type <xsl:value-of select="$ServiceName"/>Type);

      [OperationContract]
      void EnableContinuation(string activityId, string continuationToken);

      [OperationContract]
      void AddActitivityReference(string activityId, string referenceActivityName, string referenceActivityId);

      [OperationContract]
      void AddReference(string activityId, string referenceType, string referenceName, string referenceData);

      [OperationContract]
      void AddLongReference(string activityId, string referenceType, string referenceName, string referenceData, string longReferenceData);

      [OperationContract]
      void EndActivity(string activityId);
    }
  </xsl:template>
</xsl:stylesheet>
